package com.manor.paradise.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.manor.common.http.JHttpCallback
import com.manor.common.utils.GsonUtil
import com.manor.common.utils.KLog
import com.manor.common.utils.ToastUtil
import com.manor.paradise.R
import com.manor.paradise.adapter.SpiritAdapter
import com.manor.paradise.bean.MyHireBean
import com.manor.paradise.http.ElvesHttpConst
import com.manor.paradise.http.ElvesHttpUtil
import kotlinx.android.synthetic.main.fragment_my_fairy.*
import java.util.*


/**
 * 我的精灵
 */
class MyFairyFragment : Fragment() {
    var mCurView: View? = null
    internal var arr: ArrayList<String> = ArrayList()
    internal lateinit var mAdapter: SpiritAdapter
    internal var pageNum = 1

    companion object{
        fun newInstance(): MyFairyFragment {
            return MyFairyFragment()
        }
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        mCurView = inflater.inflate(R.layout.fragment_my_fairy, container, false)
        var rv = mCurView?.findViewById<RecyclerView>(com.manor.paradise.R.id.rcv_list)
        return mCurView
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        rcv_list.layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
        mAdapter = SpiritAdapter(R.layout.item_spirit, null)
        rcv_list.adapter = mAdapter

        refreshLayout.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onLoadMore(refreshLayout: RefreshLayout) {
                pageNum++
                setData()
            }

            override fun onRefresh(refreshLayout: RefreshLayout) {
                pageNum = 1
                setData()
            }
        })
    }

    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        super.setUserVisibleHint(isVisibleToUser)
        if (isVisibleToUser) {
            setData()
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    fun setData() {
        ElvesHttpUtil.getMyhire(pageNum, object : JHttpCallback() {
            override fun onSuccess(code: Int, msg: String, info: String) {
                if (code != 200) {
                    ToastUtil.show(msg)
                    return
                }
                var shopListBean: MyHireBean = GsonUtil.gsonToBean<MyHireBean>(info, MyHireBean::class.java)
                KLog.e("json", shopListBean)
                if (pageNum == 1) {

                    mAdapter.setNewInstance(shopListBean.records)

                    refreshLayout?.finishRefresh()
                    if (shopListBean.records.size == 0) {
                        mEmptyView?.showNull()
                    } else {
                        mEmptyView?.visibility = View.GONE
                    }
                } else {
                    mAdapter.addData(shopListBean.records)
                    refreshLayout?.finishLoadMore()
                }

            }

        })
    }

    override fun onDestroy() {

        ElvesHttpUtil.cancel(ElvesHttpConst.POST_OPEN_HIRE_LIST)
        super.onDestroy()
    }

}